<?php
 /*******************************************************************************************/
 /*		说明			:	分页类															*/
 /*		功能			:	根据提供的参数列出分页											*/
 /*		输入			:																	*/
 /*		输出			:																	*/
 /*		作者			:	陈冬冬(chendd@fjii.com)											*/
 /*		版本			:	1.00.20030411													*/
 /*		文件名			:	pages.inc.php													*/
 /*		最后修改时间	:	2003-04-11 09:39:52												*/
 /*		备注			:																	*/
 /*******************************************************************************************/


class pages
{
	var $intPage;
	var $start;
	var $lim=20;
	var $zapros;
	var $query;
	var $type="extend";//normal
	var $pageAjax = 0;
	var $ajaxFunName = "ajax2page";

	var $script;
	var $vars;
	var $titlenext="向后";
	var $titleprev="向前";
	var $center=true;
	var $htmlClass;
	var $listNum=10;
	var $intNext;
	var $strMakeList;
	
	function pages()
	{
		if(empty($this->intNext))
		{
			global $intNext;
			if(!isset($intNext))
			{
				$this->intNext=1;
			}
			else
			{
				$this->intNext=$intNext;
			}
		}
		
		if(empty($this->db))
		{
			global $DB;
			$this->db = $DB;			
		}
		
		if(empty($this->start))
		{
			global $start;
			global $intPage;
			$intPage = $_REQUEST["intPage"];
			if(empty($intPage))
			{
				$this->intPage=1;
			}
			else
			{
				$this->intPage=$intPage;
			}
			$this->start = ($this->intPage-1)*$this->lim;
			
			if(empty($this->start))
			{
				$this->start=0;
			}
		}
		else
		{
			$this->start = ($this->intPage-1)*$this->lim;
			//echo "*".$this->start.$this->lim."*";
			$intTemp=($this->start/$this->lim+1)/$this->listNum;
			if(($intTemp - intval($intTemp))>0) {
				$this->intNext=intval($intTemp)+1;
			}else{
				$this->intNext=intval($intTemp);
			}
		}
		
		if(empty($this->script))
		{			
			global $SCRIPT_NAME;
			global $QUERY_STRING;
			//global $_SERVER['PHP_SELF'];
			$SCRIPT_NAME=$_SERVER['PHP_SELF'];
			$QUERY_STRING=$_SERVER['QUERY_STRING'];
			$this->script= basename($SCRIPT_NAME)."?".$QUERY_STRING;
			$intPosTag=strpos($this->script,"&intPage");
			if(is_int($intPosTag))
			{
				$this->script=substr($this->script,0,$intPosTag);
			}
			$intPosTag=strpos($this->script,"&intNext");
			if(is_int($intPosTag))
			{
				$this->script=substr($this->script,0,$intPosTag);
			}
		}
		//echo $this->script;
		//echo("<br>");
		//echo($_SERVER['QUERY_STRING']);
		$bolAddCha=strpos($this->script,"?");
		if($bolAddCha===false)
		{
			$this->script=$this->script."?";
		}
		else
		{
			if(substr($this->script,-1,1)!="&")
			{
				$this->script=$this->script."&";
			}
		}
		if(empty($this->pageAjax)){
			global $pageAjax;
			$this->pageAjax = $pageAjax;
		}
		//echo $this->script;
	}
	
	function check()
	{
		$query = (empty($this->zapros)? $this->query:$this->zapros);
		
		$res=$this->db->Execute($query);		
		$cnt=$res->RecordCount();
				
		$res_lim=$this->db->SelectLimit($query,$this->lim,$this->start);
		$do[0]=$res_lim;
		if($cnt > $this->lim)
		{
			$do[1]=true;
		}
		else
		{
			$do[1]=false;
		}	
		$do[2]=$cnt;
		
		
		///执行分页操作
		$start=$this->start;
		$lim=$this->lim;
		$listNum=$this->listNum;
		$intNext=$this->intNext;
		$intPage=$this->intPage;
		$allrec=$do[2];		
		if($do[1])
		{
			if($this->center) $strOutput="<div align=\"center\">";
			$page=($start/$lim)+1;
			if($allrec%$lim!=0)
			{
				$allpages=(intval($allrec/$lim))+1;
			}
			else
			{
				$allpages=intval($allrec/$lim);
			}
			$last = $start-$lim;
			$next = $start+$lim;
			if(is_array($this->vars))
			{
				foreach($this->vars as $key=>$val)
				{
					$suffix.="&$key=$val";
				} 
			}
			
			if($this->type=="extend")
			{
				
				//$nowpage=($start/$lim+1);
				$strOutput = $strOutput."<style> .px12 {font-family: \"宋体\", \"Microsoft Sans Serif\", \"Verdana\", \"Arial\";font-size: 12px;}</style>\n<table border=0 class=\"".$this->htmlClass."\">\n";
				$strOutput = $strOutput."<form method=post name=frmChange onsubmit='return goPage();'><tr align=center class='px12'>";
				$strOutput = $strOutput."\n\t<td width=30% valign=bottom>\n共".$allrec."条 共".$allpages."页 当前第".$intPage."页\n\t</td>\n";
				$start? $strOutput = $strOutput."\t<td width=30% valign=bottom><a href='".$this->script."intPage=1' title='首页'>首页</a>\n":$strOutput = $strOutput."\t<td valign=bottom>首页 \n";
				($intPage!=1)?$strOutput = $strOutput."<a href='".$this->script."intPage=".($intPage-1)."' title='上一页'>上一页</a>\n":$strOutput = $strOutput."上一页\n";
				($intPage>=$allpages)?$strOutput = $strOutput."下一页\n":$strOutput = $strOutput."<a href='".$this->script."intPage=".($intPage+1)."' title='下一页'>下一页</a>\n";
				($intPage==$allpages)?$strOutput = $strOutput." 末页</td>":$strOutput = $strOutput." <a href='".$this->script."intPage=".$allpages."' title='末页'>末页</a></td>";
				$strOutput = $strOutput."<script language='javascript'>\n";
				$strOutput = $strOutput."function goPage()\n";
				$strOutput = $strOutput."{\n";
				$strOutput = $strOutput."var frm=document.frmChange;\n";
				$strOutput = $strOutput."var intNowPage=frm.intPage.value;\n";
				$strOutput = $strOutput."var intStandard=/^[0-9]{1,10}$/;\n";
				$strOutput = $strOutput."if(intStandard.test(intNowPage)){\nif(intNowPage<=0 || intNowPage>".$allpages."){\n alert('请输入合法的页码！');\n return false;\n}\n}\n";
				$strOutput = $strOutput."else{return false;}";
				$strOutput = $strOutput."window.location.href='".$this->script."intPage='+intNowPage;";
				$strOutput = $strOutput."}\n";
				$strOutput = $strOutput."</script>";
				$strOutput = $strOutput."<td width=30% valign=bottom>转到：第<input type=text name='intPage' size=3 class='input'>页<input type=submit value='GO' style='border: 1px black solid;'></td></tr>\n</form></table>";				
				//$strOutput = $strOutput."";
			}
			else if($this->type=="normal")
			{
				$dNum=$start/$lim;
				
				if($allpages>$listNum)
				{
					if($intNext*$listNum>$allpages)
					{
						$intPageMax=$allpages;
						$intPageMin=($intNext-1)*$listNum+1;
					}
					else
					{
						$intPageMax=$intNext*$listNum;
						$intPageMin=($intNext-1)*$listNum+1;
					}
				}
				else
				{
					$intPageMax=$allpages;
					$intPageMin=($intNext-1)*$listNum+1;
				}
				if($allpages%$listNum!=0)
				{
					$lastNumPage=intval($allpages/$listNum)+1;
				}
				else
				{
					$lastNumPage=intval($allpages/$listNum);
				}
				
				if($this->pageAjax){
					//使用AJAX分页跳转
					$js_fun = $this->ajaxFunName;
					($intNext==1?$strOutput = $strOutput."共".$allrec."条 共".$allpages."页 当前第". intval($intPage) ."页 \n<font face=webdings>9</font> ":$strOutput = $strOutput."共".$allrec."条 共".$allpages."页 当前第".$intPage."页\n <a href=\"javascript:".$js_fun."('".$this->script."intNext=".($intNext-1)."&intPage=".(($intNext-1)*$listNum-1)."');\" title='前10页'><font face=webdings >9</font></a> ");
				$strOutput.= ($page > 1 ? "<a title=\"$this->titleprev\" href=\"javascript:".$js_fun."('".$this->script."&intPage=$dNum$suffix');\"><font face=webdings>3</font></a>\n" : "<font face=webdings>3</font>");
					for($n=$intPageMin;$n<=$intPageMax;$n++)
					{
						($n==$page ? $strOutput = $strOutput." $n" : $strOutput = $strOutput." <a href=\"javascript:".$js_fun."('".$this->script."intPage=$n$suffix');\" title=\"$n\">$n</a>\n");
					}
					$uNum=$next/$lim+1;
				$strOutput.= ($page < $allpages ? "<a title=\"$this->titlenext\" href=\"javascript:".$js_fun."('".$this->script."&intPage=$uNum$suffix');\"><font face=webdings>4</font></a>\n" : "<font face=webdings>4</font>");
					($intNext==$lastNumPage?$strOutput = $strOutput." <font face=webdings>:</font>":$strOutput = $strOutput."<a href=\"javascript:".$js_fun."('".$this->script."intNext=".($intNext+1)."&intPage=".(($intNext+1)*$listNum-1)."');\" title='后10页'><font face=webdings>:</font></a> ");
				
				}else{
					//常规分页
					($intNext==1?$strOutput = $strOutput."共".$allrec."条 共".$allpages."页 当前第".$intPage."页\n<font face=webdings>9</font> ":$strOutput = $strOutput."共".$allrec."条 共".$allpages."页 当前第".$intPage."页\n <a href='".$this->script."intNext=".($intNext-1)."&intPage=".(($intNext-1)*$listNum-1)."' title='前10页'><font face=webdings >9</font></a> ");
				$strOutput.= ($page > 1 ? "<a title=\"$this->titleprev\" href=\"".$this->script."&intPage=$dNum$suffix\"><font face=webdings>3</font></a>\n" : "<font face=webdings>3</font>");
					for($n=$intPageMin;$n<=$intPageMax;$n++)
					{
						($n==$page ? $strOutput = $strOutput." $n" : $strOutput = $strOutput." <a href=\"".$this->script."intPage=$n$suffix\" title=\"$n\">$n</a>\n");
					}
					$uNum=$next/$lim+1;
				$strOutput.= ($page < $allpages ? "<a title=\"$this->titlenext\" href=\"".$this->script."&intPage=$uNum$suffix\"><font face=webdings>4</font></a>\n" : "<font face=webdings>4</font>");
					($intNext==$lastNumPage?$strOutput = $strOutput." <font face=webdings>:</font>":$strOutput = $strOutput."<a href='".$this->script."intNext=".($intNext+1)."&intPage=".(($intNext+1)*$listNum-1)."' title='后10页'><font face=webdings>:</font></a> ");
				}
			}
			
			if($this->center) $strOutput = $strOutput."</div>";
		}
		if(empty($strOutput)){
			$strOutput = $strOutput."共".$allrec."条 共1页 当前第". intval($intPage) ."页";
		}
		
		$do[3]=$strOutput;
		$this->strMakeList=$strOutput;
		return $do;
	}
	
	function make_list()
	{
		echo $this->strMakeList;
	}
	
}
//****************************
?>